<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends BaseController
{
    public function add()
    {
    	if(IS_POST)
    	{
    		$model = D('User');
    		if($model->create(I('post.'), 1))
    		{
    			if($id = $model->add())
    			{
    				$this->success('添加成功！');
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}

		// 设置页面中的信息
		$this->assign(array(
		));
		$this->display();
    }
    public function edit()
    {
    	$id = I('get.id');
    	if(IS_POST)
    	{
    		$model = D('Admin/User');
    		$data = I('post.');
    			if($model->save($data) !== FALSE)
    			{
    				$this->success('修改成功！', U('index', array('p' => I('get.p', 1))));
    				exit;
    			}


    		$this->error($model->getError());
    	}
    	$model = M('User');
    	$data = $model->find($id);
        // 设置页面中的信息
        $this->assign(array(
            '_page_title' => '修改散客',
        ));
    	$this->assign('data', $data);

		$this->display('edit');
    }
    public function delete()
    {
    	$model = D('User');
    	if($model->delete(I('get.id', 0)) !== FALSE)
    	{
    		$this->success('删除成功！', U('index', array('p' => I('get.p', 1))));
    		exit;
    	}
    	else 
    	{
    		$this->error($model->getError());
    	}
    }
    public function index()
    {
        if(IS_POST){
            $model = D('User');
            $data = $model->search();
            var_dump($data);
            $this->assign(array(
                'data' => $data['data'],
                'page' => $data['page'],
            ));
            $this->display();
        }else{
            $model = D('User');
            $data = $model->search();
            $this->assign(array(
                'data' => $data['data'],
                'page' => $data['page'],
            ));
            $this->display();
        }
    }
	public function user_show()
	{
		$model = D('User');
		$data = $model->search();
		$this->assign(array(
			'data' => $data['data'],
			'page' => $data['page'],
		));
		$this->display();
	}

	public function isPass()
	{
	    if(IS_AJAX){
            $id = I('get.id');
            $model = D('User');
            $data['is_pass'] = 1;
            $result=$model->where([
                'id' =>['eq',$id]
            ])->save($data);
            if($result){
                return true;
            }else
                return false;
        }
		return false;
	}
    public function downPass()
        {
            if(IS_AJAX){
                $id = I('get.id');
                $model = D('User');
                $data['is_pass'] = 0;
                $result=$model->where([
                    'id' =>['eq',$id]
                ])->save($data);
                if($result){
                    return true;
                }else
                    return false;
            }
            return false;
        }


    /**
     *
     */
    public function add_file()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        //$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Public/upload/sanke/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件
        $info   =   $upload->upload();
        $file = './Public/upload/sanke/'.$info['import']['savepath'].$info['import']['savename'];

        $data = import_excel($file);
        $count =  0;
        $ncount = 0;
        foreach ($data as $k=>$v)
        {
            if($k>1)
            {
                $arr['username'] = $v['0'];
                $arr['telphone'] = $v['1'];
                $arr['company_id']= session('admin.company_id');
                $res = M("member")->where($arr)->find();
                if(!$res)
                {
                    $arr['remarks']  = $v['2'];
                    M("User")->data($arr)->add();
                    $count++;
                }else
                {
                    $ncount++;
                }

            }
        }

        $this->success($ncount."条数据重复"."/".$count."条数据导入成功！");
    }
}